Imports adsiClass
Imports Crypto
Imports System.DirectoryServices

Partial Class wpAdminResetPassword
    Inherits System.Web.UI.Page

    Dim ad As New adsiClass
    Dim cr As New Crypto

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.lblLoginName.Text = Session("loginName").ToString
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        If Me.txtNewPassword.Text.ToString <> "" Then
            ad.userName = cr.Decrypt(Session("user").ToString, "key1!")
            ad.userPassword = cr.Decrypt(Session("pas").ToString, "key1!")
            Dim loginName As String = ad.ExtractUserNameFromLoginName(Me.lblLoginName.Text.ToString)
            Dim result As SearchResult
            '''''''Declare Username and Password before you make this object...
            Dim de As DirectoryEntry = ad.GetDirectoryEntry("LDAP://" & cr.Decrypt(Session("dc").ToString, "key1!") & "")
            Dim dsh As New DirectorySearcher(de)
            dsh.SearchScope = SearchScope.Subtree
            dsh.Filter = "(&(objectClass=user) (sAMAccountName=" & loginName & "))"
            Try
                result = dsh.FindOne()
                ad.SetPassword(result.Path, Me.txtNewPassword.Text.ToString)
            Catch ex As Exception
                ad.SaveTextToFile(Now.ToLongDateString & " " & Now.ToLongTimeString & vbNewLine & ex.Message.ToString & vbNewLine & ex.ToString & vbNewLine & vbNewLine & ex.StackTrace.ToString & vbNewLine & vbNewLine & "DB Connectivity Error" & vbNewLine)
                Session("msg") = "Access denied. You are not authorized to change some data. Please contact system administrator..."
                Response.Redirect("~/wpMessage.aspx")
            End Try
        End If
    End Sub

End Class
